<?php

/**
 * Controller_discipline
 * 学科组
 */

class Controller_discipline extends MyApp {

	public function __construct()
	{

		parent::__construct();

		$this->model->staff    		= YOU::loadModel('staff');
		$this->model->appoint    	= YOU::loadModel('appoint');
		$this->model->grade    		= YOU::loadModel('grade');
		$this->model->discipline    = YOU::loadModel('discipline');
		$this->model->log           = YOU::loadModel('log');

	}
	/** 首页 **/
	public function actionIndex()
	{

		$this->tpl->display();
	}
	/** 学科组列表 **/
	public function actionDisciplineList()
	{
		// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);
		
		// 所有老师
		$staff_lists = $this->model->staff->getAll();
		// 分页
		YOU::loadLib('page', "", '0');
		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_discipline`");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);
		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}

		$discipline_lists = $this->db->fetchAll("SELECT * FROM `tbl_discipline` ORDER BY discipline_id DESC LIMIT {$start}, ".PAGE_LIMIT."");
		foreach ($discipline_lists as $key => $value){
			// 普通员工
			$row  = $this->model->appoint->getDisc($value['discipline_id']);
			foreach ($row as $k => $v){
				// 员工姓名
				$rows  = $this->model->staff->getinfo($v['staff_id']);
				$discipline_lists[$key]['staff_name'][$v['staff_id']] = $rows['staff_name'];
			}
		}
		// 添加
		if ($_POST) {
			$discipline_id     		= $this->post->discipline_id;
			$staff_id     			= $this->post->staff_id;

			foreach ($staff_id as $key => $value){

				// 查找任职表
				$appoint_data =$this->model->appoint->getDiscRow($value,$discipline_id);
				if (!empty($appoint_data)) {

					// 写入数据库
					$info = array(

					"discipline_id"      	=> $discipline_id,
					"staff_id"      		=> $value,
					"appoint_common"      	=> 1,
					"create_time"           => date("Y-m-d H:i:s"),

					);
					$this->db->do_update("tbl_appoint", $info, "appoint_id='{$appoint_id}'");
				}else {

					// 写入数据库
					$info = array(

					"discipline_id"      	=> $discipline_id,
					"staff_id"      		=> $value,
					"appoint_common"      	=> 1,
					"status"                => 1,
					"create_time"           => date("Y-m-d H:i:s"),
					"update_time"           => "0000-00-00 00:00:00",

					);

					$this->db->do_insert("tbl_appoint", $info);
				}
			}
			Helper_Client::location("添加成员成功", "index.php?a=discipline.disciplinelist");
		}

		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";
		$this->tpl->assign("page", $page->show(4, "yahoo"));

		$this->tpl->assign("discipline_lists", $discipline_lists);
		$this->tpl->assign("staff_lists", $staff_lists);
		$this->tpl->display('discipline/discipline.list.tpl.php');
	}
	/** 学科组人员添加 **/
	public function actionStaffAdd()
	{
		// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);
		
		$discipline_id     		= $this->get->discipline_id;
		// 部门名称
		$discipline_data =$this->model->discipline->getinfo($discipline_id);

		// 查找任职表
		$appoint_data =$this->model->appoint->getDisc($discipline_id);
		// 所有老师
		$staff_lists = $this->model->staff->getAll();
		foreach ($staff_lists as $key=> $value){
			foreach ($appoint_data as $ke=>$va){
				if ($value['staff_id']==$va['staff_id']) {
					//print_R($value['staff_name']);
					$value['is_she'] ==1;
					$staff_lists[$key]['is_she']=1;

				}
			}
		}

		if ($_POST){

			$staff_id     			= $this->post->staff_id;

			// 先删除 在存入
			if (!empty($discipline_id)) {
				$this->db->query("DELETE  FROM `tbl_appoint` WHERE appoint_common='1' AND discipline_id='{$discipline_id}'");
			}
			if (!empty($staff_id)) {
				foreach ($staff_id as $key => $value){

					// 写入数据库
					$info = array(

					"discipline_id"      	=> $discipline_id,
					"staff_id"      		=> $value,
					"appoint_common"      	=> 1,
					"status"                => 1,
					"create_time"           => date("Y-m-d H:i:s"),
					"update_time"           => "0000-00-00 00:00:00",

					);

					$this->db->do_insert("tbl_appoint", $info);

				}
			}

			Helper_Client::location("添加成员成功", "index.php?a=discipline.disciplinelist");


		}

		$this->tpl->assign("staff_lists", $staff_lists);
		$this->tpl->assign("discipline_data", $discipline_data);
		$this->tpl->display('discipline/discipline.staff.tpl.php');


	}
	/** 学科组添加 **/
	public function actionDisciplineAdd()
	{
		// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);
		
		if ($_POST){

			$discipline_name     	= $this->post->discipline_name;

			// 写入数据库
			$info = array(

			"discipline_name"      	=> $discipline_name,
			"status"                => 1,
			"create_time"           => date("Y-m-d H:i:s"),
			"update_time"           => "0000-00-00 00:00:00",

			);
			$this->db->do_insert("tbl_discipline", $info);

			// 跳转
			Helper_Client::location("添加成功", "index.php?a=discipline.disciplinelist");
		}


		$this->tpl->display('discipline/discipline.add.tpl.php');
	}
	/** 学科组修改 **/
	public function actionDisciplineUpdate()
	{
		
		// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);
		
		$discipline_id   = (int)$this->get->discipline_id;
		$discipline_data = $this->db->fetchROW("SELECT * FROM `tbl_discipline` WHERE `discipline_id`='{$discipline_id}'");

		if (empty($discipline_data)){
			Helper_Client::alert("年级不存在");
		}

		if ($_POST){

			$discipline_name     	    = $this->post->discipline_name;

			// 写入数据库
			$info = array(

			"discipline_name"      	=> $discipline_name,
			"status"                => 1,
			"create_time"           => date("Y-m-d H:i:s"),
			"update_time"           => "0000-00-00 00:00:00",
			);
			$this->db->do_update("tbl_discipline", $info, "discipline_id='{$discipline_id}'");

			// 跳转
			Helper_Client::location("	修改成功", "index.php?a=discipline.disciplineList");
		}

		$this->tpl->assign("discipline_data", $discipline_data);
		$this->tpl->display('discipline/discipline.update.tpl.php');
	}
	/** 学科组删除 **/
	public function actionDisciplineDel()
	{
		$discipline_id = $this->get->discipline_id;
		$this->db->query("DELETE  FROM `tbl_discipline` WHERE discipline_id='{$discipline_id}'");

		// 跳转
		Helper_Client::location("删除成功", "index.php?a=discipline.disciplineList");
	}
	/** 学科组批量生成 **/
	public function actiondisciplinebatch()
	{
		// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);
		
		$grade_list      = $this->model->grade->getall();
		$subject_list    = $this->db->fetchAll("SELECT `subject_name`,`subject_id` FROM `tbl_subject`");

		if ($_POST){

			$grade_id     	  = $this->post->grade_id;
			$subject_id      = $this->post->subject_id;
			$discipline_name   = $this->post->discipline_name;
			$grade_date      = $this->db->fetchROW("SELECT `grade_name` FROM `tbl_grade` WHERE grade_id='{$grade_id}'");
			$subject_date    = $this->db->fetchROW("SELECT `subject_name` FROM `tbl_subject` WHERE subject_id='{$subject_id}'");
			$subject_name    = $subject_date['subject_name'];
			$grade_name      = $grade_date['grade_name'];
			// 批量添加
			if($grade_id == 0){
				foreach ($grade_list as $k => $l){
					$grade_name= $l['grade_name'];
					$info = array(

					"discipline_name"      	=> $grade_name.$subject_name.'学科组',
					"status"                => 1,
					"create_time"           => date("Y-m-d H:i:s"),
					"update_time"           => "0000-00-00 00:00:00",

					);
					$this->db->do_insert("tbl_discipline", $info);
				}

			}else{
				//  单条添加
				$info = array(

				"discipline_name"      	=> $grade_name.$subject_name.'学科组',

				"status"                => 1,
				"create_time"           => date("Y-m-d H:i:s"),
				"update_time"           => "0000-00-00 00:00:00",

				);

				$this->db->do_insert("tbl_discipline", $info);
			}
			// 跳转
			Helper_Client::location("批量添加成功", "index.php?a=discipline.disciplinelist");
		}
		$this->tpl->assign("grade_list", $grade_list);
		$this->tpl->assign("subject_list", $subject_list);
		$this->tpl->display('discipline/discipline.batch.tpl.php');
	}

}

?>